;thread for lag screws
;outputs; [body]
;
;simplified thread?
{If:[sel_th]=Simplified}
	{If:[sel_reduced]=False}
		_Polyline _PersistentClose=_No 0,[d]/2,[tipstart] _Mode=_Line 0,[d]/2,[tipend] 0,0,-[l] _Enter
	{EndIf:}
	{If:[sel_reduced]=True}
		_Polyline _PersistentClose=_No 0,[dminth]/2,[tipstart] _Mode=_Line 0,[d]/2,[tipstart]-(([d]-[dminth])/2) 0,[d]/2,[tipend] 0,0,-[l] _Enter
	{EndIf:}	
	_SelLast
	{Revolve:0,0,0:0,0,[l]}
	{SetName:[nd]}
	{Set:[thread]={SelectedObjs}}
	_SelNone
{EndIf:}
;
;detailed thread?
{If:[sel_th]=Detailed}
	;
	;main thread spiral
	{If:[sel_reduced]=False}
		{Set:[turns]={Math:(abs([tipend]-[tipstart])/[pth])+0.5}}
	{EndIf:}
	{If:[sel_reduced]=True}
		{Set:[turns]={Math:(abs([tipend]-[tipstart])/[pth])-1}}
	{EndIf:}
	_Spiral 0,0,[tipend] 0,0,[tipend]+([turns]*[pth]) _Mode=_Turns _Turns=[turns] 0,[d]/2,[tipend] 0,[d]/2,[tipend]+([turns]*[pth]) _Enter
	_SelLast
	{Set:[spiral]={SelectedObjs}}	
	_SelNone
	;
	;thread section
	{Set:[SectionID]={Guid}}
	_Polyline _PersistentClose=_No 0,([dminth]/2)-([pth]*0.3),[tipend] _Mode=_Line 0,([dminth]/2)-([pth]*0.3),[tipend]+(tan(30d)*((([d]-[dminth])/2)+([pth]*0.3))) 0,[d]/2,[tipend] 0,([dminth]/2)-([pth]*0.3),[tipend]-(tan(30d)*((([d]-[dminth])/2)+([pth]*0.3))) _Close
	_SelLast
	{Set:[section]={SelectedObjs}}
	{SetName:[SectionID]}
	{Duplicate}
	_Rotate _Copy=_No 0 {ObjectSnap:[spiral]:StartXY} {ObjectSnap:[spiral]:EndXY}
	_Move 0,0,{ObjectSnap:[spiral]:StartZ} 0,0,{ObjectSnap:[spiral]:EndZ}
	{Set:[section2]={SelectedObjs}}
	_SelNone
	;
	;thread end at tip
	_Spiral 0,0,[tipend] 0,0,[tipend]-([pth]/2) _Mode=_Turns _Turns=0.5 0,[d]/2,[tipend] 0,([dminth]/2)-(tan(30d)*([pth]/2))-[tol],[tipend]-([pth]/2) _Enter
	_SelLast
	{Set:[spiral2]={SelectedObjs}}
	_SelNone
	;
	;thread section
	_SelId [section]
	{Duplicate}
	{Set:[section3]={SelectedObjs}}
	_Rotate _Copy=_No 0 {ObjectSnap:[spiral]:StartXY} {ObjectSnap:[spiral2]:EndXY}
	_Move {ObjectSnap:[section3]:Middle} {ObjectSnap:[spiral2]:End}
	_Scale _Copy=_No {ObjectSnap:[spiral2]:End} 0.25 _Enter
	_SelNone
	;
	;thread end at head
	{If:[sel_reduced]=True}
		_Spiral 0,0,{ObjectSnap:[spiral]:EndZ} 0,0,{ObjectSnap:[spiral]:EndZ}+[pth] _Mode=_Turns _Turns=1 {ObjectSnap:[spiral]:End} {ObjectSnap:[spiral]:EndXY},{ObjectSnap:[spiral]:EndZ}+[pth] _Enter
		_SelLast
		{Set:[spiral3]={SelectedObjs}}
		_SelNone
		;
		_SelId [section]
		{Duplicate}
		_Scale _Copy=_No {ObjectSnap:[spiral]:Start} 0.25 _Enter
		_Move 0,[d]/2,0 0,([dminth]/2)-([tol]*2),0
		_Rotate _Copy=_No 0 {ObjectSnap:[spiral]:StartXY} {ObjectSnap:[spiral3]:EndXY}
		_Move 0,0,{ObjectSnap:[spiral]:StartZ} 0,0,{ObjectSnap:[spiral3]:EndZ}
		{Set:[section4]={SelectedObjs}}
		_SelNone
		;
		_SelId [spiral3]
	{EndIf:}
	;
	_SelId [spiral]
	_SelId [spiral2]
	;
	_Join
	{Set:[spiral]={SelectedObjs}}
	_SelNone
	;
	{If:[sel_reduced]=True}
		-_Sweep1 '_SelID [spiral] '-_SelId [section3] '-_SelId [section] '-_SelId [section2] '-_SelId [section4] _Enter _Natural _Enter _Style=_Roadlike 0,0,0 0,0,[tipend] _Simplify=_None _Closed=_No _ShapeBlending=_Local _Enter
	{EndIf:}
	{If:[sel_reduced]=False}
		-_Sweep1 '_SelID [spiral] '-_SelId [section3] '-_SelId [section] '-_SelId [section2] _Enter _Natural _Enter _Style=_Roadlike 0,0,0 0,0,[tipend] _Simplify=_None _Closed=_No _ShapeBlending=_Local _Enter
	{EndIf:}
	_SelLast
	{Cap:[tol]}
	{Set:[thread]={SelectedObjs}}
	_SelNone
	;
	;cut the top to the neck chamfer
	{If:[sel_reduced]=False}
		_Circle _2Point -[d],0,[tipstart] [d],0,[tipstart]
		_SelLast
		{ExtrudeCrv:[tol]:0,0,{Math:[pth]*2}}
		{Set:[obj]={SelectedObjs}}
		_SelNone
		{BooleanDifference:[tol]:[thread]:[obj]}
		{Set:[thread]={SelectedObjs}}
		_SelNone
	{EndIf:}
	;
	'_SelID [thread]
	'_SelID [body]
	{BooleanUnion:[tol]}
	{Set:[body]={SelectedObjs}}
	_SelNone
	;
	;delete the sweep curves
	_SelID [spiral]
	_Delete
	-_SelName [SectionID]
	_Delete
{EndIf:}